const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');
//引用自定义
//const commte=require('../../libs/commtle');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//========================
//业务逻辑
module.exports = function() {
    var router = express.Router();
    //开始渲染后台首页
    router.get('/index.html',function(req,res,next){
        res.render("admin/index.ejs");
    });
    //查询管理员登陆
    router.get('/index.html',function(req,res,next){
        db.query('SELECT * FROM admininfo WHERE adminId='+ +req.session['userId']+'',function (err,data) {
            if(err)
            {
                res.status(400).send('<script>alert("出错哦");window.location.href="404.html";</script>').end();
            }else {
                if (data.length == 0)
                {
                    res.status(400).send('<script>alert("没有当前用户");window.location.href="404.html";</script>').end();
                }else
                {
                    req.uerinfo=data;
                    next();
                }
            }
        })


    })
    // router.get('/index.html',function(req,res,next){
    //     res.render("admin/public.ejs",{uercont:req.uerinfo});
    // });
    //404页面
    router.get('/404.html',function(req,res,next){
        res.render("admin/404.ejs");
    });
    return router;
}